let express = require("express");
let router = express.Router();
let loginVerify = require("../middlware/loginVerify");

let { Comment } = require("../models");

//接口

// 发布评论
// 需要登录
// 参数
// 评论内容
// 请求头传入token --> 评论人id ***
// 文章id
router.post("/", loginVerify, async function (req, res) {
  req.body.article_id;
  req.body.content;
  let com = new Comment({ ...req.body, reply_user_id: req.uid });
  await com.save();

  res.json({ code: 1, msg: "发布评论" });
});

// 查看某个文章评论
router.get("/:article_id", async function (req, res) {
  console.log(req.params.article_id);
  let cms = await Comment.find({ article_id: req.params.article_id }).populate(
    "reply_user_id",
    { password: 0 }
  );

  res.json({ code: 1, msg: "查看某个文章评论", data: cms });
});

module.exports = router;
